﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OperaHouse.Business.Contacts;
using OperaHouse.Configuration;
using OperaHouse.Domain.Contacts;
using Telerik.Web.Mvc;
using OperaHouse.Web.Areas.Admin.Models;
using OperaHouse.Common;

namespace OperaHouse.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class ContactController : BaseController
    {
        #region Fields
        private readonly IContactService _contactService;
        private readonly int contactPagesize;
        #endregion

        #region Constructors
        public ContactController(IContactService contactService)
        {
            this._contactService = contactService;
            this.contactPagesize = GeneralSetting.AdminPageSize;
        }
        #endregion

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            var contact = _contactService.GetAllContact(0, contactPagesize);
            var gridModel = new GridModel<ContactModel>
            {
                Data = contact.Select(x =>
                {
                    ContactModel m = new ContactModel();
                    m.Id = x.Id;
                    m.Name = x.Name;
                    m.Phone = x.Phone;
                    m.Address = x.Address;
                    m.Email = x.Email;
                    string body = CommonHelper.StripTagsCharArray(x.Body);
                    m.Body = body.Length < 100 ? body : body.Substring(0, 100);
                    m.CreatedOn = x.CreatedOn;
                    return m;
                }),
                Total = contact.TotalCount
            };
            return View(gridModel);
        }

        [HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult List(GridCommand command)
        {
            var contact = _contactService.GetAllContact(command.Page - 1, command.PageSize);
            var gridModel = new GridModel<ContactModel>
            {
                Data = contact.Select(x =>
                {
                    ContactModel m = new ContactModel();
                    m.Id = x.Id;
                    m.Name = x.Name;
                    m.Phone = x.Phone;
                    m.Address = x.Address;
                    m.Email = x.Email;
                    string body = CommonHelper.StripTagsCharArray(x.Body);
                    m.Body = body.Length < 100 ? body : body.Substring(0, 100);
                    m.CreatedOn = x.CreatedOn;
                    return m;
                }),
                Total = contact.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        public ActionResult Detail(int id)
        {
            var contactItem = _contactService.GetContactById(id);
            if (contactItem == null)
                //No contact item found with the specified id
                return RedirectToAction("List");

            ContactModel model = new ContactModel();
            model.Id = contactItem.Id;
            model.Name = contactItem.Name;
            model.Phone = contactItem.Phone;
            model.Address = contactItem.Address;
            model.Email = contactItem.Email;
            model.Body = contactItem.Body;
            model.CreatedOn = contactItem.CreatedOn;
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var contactItem = _contactService.GetContactById(id);
            if (contactItem == null)
                return Json(new
                {
                    result = "fail"
                });

            _contactService.DeleteContact(contactItem);

            return Json(new
            {
                result = "ok"
            });
        }
    }
}
